import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:hi_flutter/app_page.dart';
import 'package:http/http.dart' as http;

class PostsPage extends StatefulWidget {
  const PostsPage({Key? key}) : super(key: key);

  @override
  State<PostsPage> createState() => PostsPageState();
}

class PostsPageState extends State<PostsPage> {
  List postsList = [];

  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    return AppPage(
      body: ListView.builder(
          itemCount: postsList.length,
          itemBuilder: (BuildContext context, int position) {
            return ListTile(
              title: Text(postsList[position]["title"]),
              subtitle: Text(postsList[position]["body"]),
            );
          }),
    );
  }

  Future<void> loadData() async {
    var dataURL = Uri.parse('https://jsonplaceholder.typicode.com/posts');
    http.Response response = await http.get(dataURL);
    setState(() {
      postsList = jsonDecode(response.body);
    });
  }
}
